package com.hnucm.cooklife.ui.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.Toast;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.hnucm.cooklife.R;
import com.hnucm.cooklife.adapter.OrderAdapter;
import com.hnucm.cooklife.adapter.RecomAdapter;
import com.hnucm.cooklife.base.BaseFragment;
import com.hnucm.cooklife.base.BasePresenter;
import com.hnucm.cooklife.bean.db.OrderSec;
import com.hnucm.cooklife.contract.order.Contract;
import com.hnucm.cooklife.presenter.order.OrderPresenter;
import com.hnucm.cooklife.ui.activity.OrderDetailActivity;
import com.hnucm.cooklife.ui.activity.ShopActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import butterknife.BindView;

public class StoreFragment extends BaseFragment<Contract.IOrderView, OrderPresenter> implements Contract.IOrderView {

    private Context mContext;
    private OrderAdapter mOrderAdapter;
    private List<OrderSec> mOrderSecList = new ArrayList<>();

    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.order_recycler)
    RecyclerView mRecyclerView;

    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.store_top)
    View mView;

    @Override
    protected int getContentViewId() {
        return R.layout.store_fragment;
    }

    public static StoreFragment getInstance(){
        return new StoreFragment();
    }
    @Override
    protected void init() {
        mContext = requireActivity().getApplicationContext();
        initAdapter();
        initTop();
        mPresenter.loadOrder();
    }

    private void initTop() {
        ImageButton m1 = (ImageButton) mView.findViewById(R.id.search_expanded_back_button);
        m1.setImageResource(R.drawable.ic_add);
        ImageButton m2 = (ImageButton) mView.findViewById(R.id.search_expanded_magnifying_glass);
        m2.setImageResource(R.drawable.ic_buy);
        m2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ShopActivity.class);
                startActivity(intent);
            }
        });
    }

    private void initAdapter() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2);
        mRecyclerView.setLayoutManager(gridLayoutManager);
        mOrderAdapter = new OrderAdapter(R.layout.order_item,R.layout.clas_head_item,null);
        mOrderAdapter.setContext(mContext);
        mRecyclerView.setAdapter(mOrderAdapter);
    }

    @Override
    protected OrderPresenter createPresenter() {
        return new OrderPresenter();
    }

    @Override
    public void onLoadOrder(List<OrderSec> orderSecList) {
        mOrderSecList.clear();
        mOrderSecList.addAll(orderSecList);
        mOrderAdapter.addData(mOrderSecList);
        mOrderAdapter.setOnItemClickListener(new RecomAdapter.onItemClickListener() {
            @Override
            public void onSeleClickListener(View view, int position) {
                Intent intent = new Intent(getActivity(), OrderDetailActivity.class);
//                ToastUtils.showShort(position);
//                ToastUtils.showShort(mOrderSecList.get(position).oId);
                intent.putExtra("oId",position);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });

    }

    @Override
    public void onRefreshOrder(List<OrderSec> orderSecList) {

    }

    @Override
    public void onLoading() {

    }

    @Override
    public void onLoadingFailed() {

    }

    @Override
    public void onLoadSuccess() {

    }
}
